<?php
/**
 * grandAdminPanel - Admin Section for GRAND Media
 * 
 */
class grandAdminPanel{
	
	// constructor
	function grandAdminPanel() {
		global $pagenow;

		// Add the admin menu
		add_action('admin_menu', array(&$this, 'add_menu'));

		// Add the script and style files
		add_action('admin_print_scripts', array(&$this, 'load_scripts') );
		add_action('admin_print_styles', array(&$this, 'load_styles') );

		add_filter('contextual_help', array(&$this, 'show_help'), 10, 3);
		add_filter('screen_settings', array(&$this, 'edit_screen_meta'), 10, 2);
		if ( 'media.php' === $pagenow ) {
			add_filter('wp_redirect', array(&$this, 'gmRedirect'), 10, 2);
		}

	}

	// integrate the menu
	function add_menu()  {

		add_menu_page( __('GRAND Media Overview','gmLang'), 'GRAND Media', 8, 'grand-media', array (&$this, 'show_menu'), GRAND_URLPATH .'admin/images/grand_icon.png' );
		add_submenu_page( 'grand-dashboard' , __('GRAND Media Library', 'gmLang'), __('Media Library', 'gmLang'), 8, 'grand-media', array (&$this, 'show_menu'));

	}

	// load the script for the defined page and load only this code
	function show_menu() {
		
		global $grandLoad;
		
		// check for upgrade
		if( get_option( 'grand_db_version' ) != GRAND_DBVERSION ) {
			//return;			
		}
		
		// Set installation date
		if( empty($grandLoad->options['installDate']) ) {
			$grandLoad->options['installDate'] = time();
			update_option('grand_options', $grandLoad->options);			
		}
		
  		switch ($_GET['page']){
			case "grand-media" :
				include_once ( dirname (__FILE__) . '/functions.php' );
				include_once ( dirname (__FILE__) . '/library.php' );
				// Initate the Library page
				$grandLoad->library = new grandLibrary();
				// Render the output now, because you cannot access a object during the constructor is not finished
				$grandLoad->library->controller();
			break;
		}
	}
	
	function load_scripts() {
		
		// no need to go on if it's not a plugin page
		if( strpos(grandCore::_get('page'), 'grand') === false )
			return;

		wp_enqueue_script( 'swfobject' );
		wp_enqueue_script('dataset', GRAND_URLPATH .'admin/js/jquery.dataset.js', array('jquery'), '0.1.0');
		wp_enqueue_script('qtip', GRAND_URLPATH .'admin/js/jquery.qtip.js', array('jquery'), '2.0.0');
		wp_enqueue_script('outside-events', GRAND_URLPATH .'admin/js/outside-events.js', array('jquery'), '1.1');
		wp_enqueue_script('fancybox', GRAND_URLPATH .'admin/js/jquery.fancybox.js', array('jquery'), '1.3.4');
		wp_enqueue_script('easing', GRAND_URLPATH .'admin/js/jquery.easing.js', array('jquery'), '1.3.0');
		wp_register_script('grand-media', GRAND_URLPATH .'admin/js/grand-media.js', array('jquery'), '3.1.0');
		wp_localize_script('grand-media', 'grandMedia', array(
			'error1' => __('There are no images in the table.', 'gmLang'),
			'error2' => __('Nothing selected.', 'gmLang'),
			'loading' => GRAND_URLPATH.'admin/images/throbber.gif'
		) );
		wp_enqueue_script('grand-media');
/*
		wp_register_script('flag-ajax', FLAG_URLPATH .'admin/js/flag.ajax.js', array('jquery'), '1.4.0');
		wp_localize_script('flag-ajax', 'flagAjaxSetup', array(
					'url' => admin_url('admin-ajax.php'),
					'action' => 'flag_ajax_operation',
					'operation' => '',
					'nonce' => wp_create_nonce( 'flag-ajax' ),
					'ids' => '',
					'permission' => __('You do not have the correct permission', 'flag'),
					'error' => __('Unexpected Error', 'flag'),
					'failure' => __('A failure occurred', 'flag')				
		) );
		wp_register_script('flag-progressbar', FLAG_URLPATH .'admin/js/flag.progressbar.js', array('jquery'), '1.0.0');
		wp_register_script('swfupload_f10', FLAG_URLPATH .'admin/js/swfupload.js', array('jquery'), '2.2.0');
*/
		if (isset($_GET['page'])) { 
			switch ($_GET['page']) {
				case "grand-media" :
					wp_enqueue_script('jquery.quicksearch', GRAND_URLPATH .'admin/js/jquery.quicksearch.js', array('jquery'), '10.09.28');
				break;
			}
		}
		
	}		
	
	function load_styles() {
        // no need to go on if it's not a plugin page
		if( strpos(grandCore::_get('page'), 'grand') === false )
			return;

		wp_enqueue_style( 'qtip', GRAND_URLPATH .'admin/css/jquery.qtip.css', false, '2.0.0', 'screen' );
		wp_enqueue_style( 'fancybox', GRAND_URLPATH .'admin/css/jquery.fancybox.css', false, '1.3.4', 'screen' );
		wp_enqueue_style( 'grand-media', GRAND_URLPATH .'admin/css/grand-media.css', false, '3.1.0', 'screen' );
		switch ($_GET['page']) {
			case "grand-media" :
			break;
		}
	}
	
	function show_help($contextual_help, $screen_id, $screen) {
		// since WP3.0 it's an object
		if ( is_object($screen_id) )
			$screen_id = $screen_id->id;
		$link ='';
		switch ($screen_id) {
			case "toplevel_page_grand-media" :
			case "grand-flash_page_grand-media" :
				$link = '<a href="http://codeasily.com/wordpress-plugins/flag" target="_blank">CodEasily.com</a>'; 
			break;
		}
		if ( !empty($link) ) {
			$contextual_help  = '<p>'.sprintf(__("Get help with GRAND Media plugin on %s", 'gmLang'),$link).'</p>';
$temp = '<div class="metabox-prefs">'.$link.'</div>
<h5>'.__('More Help & Info', 'gmLang').'</h5>
<div class="metabox-prefs">
	<a href="http://codeasily.com/wordpress-plugins/flash-album-gallery/grand-flash-album-gallery-wordpress-plugin-video-tutorial" target="_blank">'.__('GRAND Media Video Tutorial', 'gmLang').'</a>
	| <a href="http://codeasily.com/wordpress-plugins/flash-album-gallery/faq" target="_blank">'.__('GRAND Media FAQ', 'gmLang').'</a>
	| <a href="http://codeasily.com/wordpress-plugins/flash-album-gallery/flag-review" target="_blank">'.__('GRAND Media Review', 'gmLang').'</a>
	| <a href="http://codeasily.com/wordpress-plugins/flash-album-gallery/languages" target="_blank">'.__('Get your language pack', 'gmLang').'</a>
	| <a href="http://photogallerycreator.com/2009/07/skins-for-flash-album-gallery" target="_blank">'. __('Flash Modules for GRAND Media', 'gmLang').'</a>
</div>'."\n";
		} 
		return $contextual_help;
	}
	
	function edit_screen_meta($current, $screen) {
		switch ($screen->id) {
			case "toplevel_page_grand-media" :
			case "grand-media_page_grand-media" :
				$current = '<h4>'.__('Settings for this page', 'gmLang').'</h4>';	
			break;
		}
		return $current;
	}

	// redirect to original referer after update
	function gmRedirect($location, $status)  {
		global $pagenow;
		if ( 'media.php' === $pagenow && isset($_POST['_wp_original_http_referer']) ) {
			if(strpos($_POST['_wp_original_http_referer'], 'grand-media') !== false) {
				return $_POST['_wp_original_http_referer'];
			} else {
				return $location;
			}
		}
	}

}

?>